﻿//------------------------------------------------------------------------------------------------------
// <summary>
//     The following code is allowed to be used inernally in Pfizer China only, 
//     you are not permitted neither to use the contents nor disclose it in any
//     manner to third parties.
// </summary>
// <copyright file="FileHelper.cs" company="Pfizer China">
//     Copyright (c) Pfizer China. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------------------------------
using System;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SystemFrameWork
{
    /// <summary>
    /// File helper
    /// </summary>
    public class FileHelper
    {
        /// <summary>
        /// Allow able file type
        /// </summary>
        /// <param name="FileUpload">file control</param>
        /// <returns>bool</returns>
        public static bool IsAllowableFileType(FileUpload fileUpload)
        {
            string fileTypeLimit = ".xml";
            string name = Path.GetExtension(fileUpload.FileName).ToLower();
            if (StringHelper.EqualIgnoreCase(fileTypeLimit, name))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// File size
        /// </summary>
        /// <param name="FileUpload">file control</param>
        /// <returns>bool</returns>
        public static bool IsAllowableFileSize(FileUpload fileUpload)
        {
            double fileSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FileSizeLimit"]);
            if (fileSizeLimit > fileUpload.PostedFile.ContentLength)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Delete file
        /// </summary>
        /// <param name="fileName">file name</param>
        /// <returns>true or false</returns>
        public static bool DeleteFile(string fileName)
        {
            try
            {
                string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString();
                ///comlpex physical path
                //string filePhysicalPath =  fileName;
                ///delete file
                System.IO.File.Delete(fileName);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void CreateFolder(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// judge the file format
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="strFileTypeLimit"></param>
        /// <returns></returns>
        public static bool IsAllowableFileType(string fileName, string strFileTypeLimit)
        {
            if (strFileTypeLimit.Contains(Path.GetExtension(fileName).ToLower()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Judge the file size
        /// </summary>
        /// <param name="fileSize">fileSize</param>
        /// <returns>bool</returns>
        public static bool IsAllowableFileSize(int fileSize)
        {
            double iFileSizeLimit = ConfigurationHelper.UploadFileSize;

            if (iFileSizeLimit > fileSize)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath">the file full path</param>
        /// <param name="file">the binary file</param>
        /// <param name="canDelete">true:replace the old file with the new file</param>
        /// Added by lixing
        public static void GenerateMediamFile(string filePath, byte[] file, bool canDelete)
        {
            if (file==null)
            {
                return;
            }
            //// Create the file is not existed
            if (!File.Exists(filePath))
            {
                using (System.IO.FileStream fs = System.IO.File.Create(filePath))
                {
                    foreach (byte i in file)
                    {
                        fs.WriteByte(i);
                    }
                }
                return;
            }

            if (canDelete)
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }

                using (System.IO.FileStream fs = System.IO.File.Create(filePath))
                {
                    foreach (byte i in file)
                    {
                        fs.WriteByte(i);
                    }
                }
                return;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        /// Added by Li Xing
        public static byte[] GetFileBinary(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return null;
            }
            byte[] readBuffer = System.IO.File.ReadAllBytes(filePath);
            return readBuffer;
        }

        /// <summary>
        /// delete a file
        /// </summary>
        /// <param name="filePath"></param>
        public static void DeleteTempFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
                return;
            }
        }

        /// <summary>
        /// check the file
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool ExistedFile(string filePath)
        {
            return File.Exists(filePath);
        }
    }
}
